import { CompatibilityTabooDrugClient as Client, CompatibilityTabooPageInput as InputGetListModel, CompatibilityTabooDrugInput as InputInsertModel, CompatibilityTabooOutput as listItemModel, TabooRulePageInput } from "@/api/api_compatibilitytaboodrugclient";
import { ApiExceptionShow } from "@/framework/utils/api-exception-show";
import notify from "devextreme/ui/notify";
import { Vue } from "vue-class-component";
/*
 * @Author: LAB
 * @Date: 2021-05-26 17:02:18
 * @Description: 药品规格
 * @FilePath: \client-app\src\data-source\drug-taboo.ts
 */
export interface ListDataI<T> {
  data: T[],
  dataCount: number;
}
export default class DrugTabooData {
  client: Client;
  view: Vue
  constructor(client: Client, view: Vue) {
    this.client = client;
    this.view = view;
  }
  // 获取配伍禁忌
  getList(body: InputGetListModel): Promise<ListDataI<listItemModel>> {
    return this.client.getPageByCompatibilityTaboo(body).then(res => {
      if (res.isSuccess) {
        console.log("res", res);
        return {
          data: res.listData,
          dataCount: res.pageInfo && res.pageInfo.count || 0
        }
      } else {
        notify(res.message, "error")
        return {
          data: [],
          dataCount: 0
        }
      }
    }, err => {
      ApiExceptionShow.showException(err, this.view)
      return {
        data: [],
        dataCount: 0
      }
    })
  }
  // 获取输入模型
  getTabooModel(id: string) {
    return this.client.getCompatibilityTabooDrugInput(id).then(res => {
      if (res.isSuccess) {
        return res.data
      } else {
        notify(res.message, "error")
        return null
      }
    }).catch(err => {
      ApiExceptionShow.showException(err, this.view)
    })
  }
  add(body: InputInsertModel) {
    return this.client.submit(body).then(res => {
      if (res.isSuccess) {
        notify(res.message, "success")
      } else {
        notify(res.message, "error")
      }
    }).catch(err => {
      ApiExceptionShow.showException(err, this.view)
    })
  }
  update(body: InputInsertModel) {
    return this.client.submit(body)
  }
  delete(id: string, drugId: string) {
    return this.client.delete(id, drugId).then(res => {
      if (res.isSuccess) {
        notify("删除成功", "success")
      } else {
        notify(res.message, "error")
      }
    }), err => {
      ApiExceptionShow.showException(err, this.view)
    }
  }
  info(id: string): Promise<any> {
    return Promise.resolve({})
  }
  // 获取禁忌列表
  getTabooList(searchText: string, showCondition: 0 | 1 | 2 = 2, body: TabooRulePageInput) {
    let params = Object.assign({
      pageInfo: {
        count: 0,
        pageSize: 10,
      },
    }, body, { searchText, showCondition })
    return this.client.getPageByTabooRule(params)
      .then((res) => {
        if (res.isSuccess) {
          return res.listData;
        } else {
          return [];
        }
      })
      .catch((err) => {
        return [];
      });
  }

  // 获取禁忌类型列表
  getTabooTypeList() {
    return this.client.getDicationForCompatibilityTaboo()
      .then((res) => {
        if (res.isSuccess) {
          return res.listData;
        } else {
          return [];
        }
      })
      .catch((err) => {
        return [];
      });
  }
}